home *** CD-ROM | disk | FTP | other *** search
- unit ntc_server_network;
- {
- Copyright (C) 2004 - 2006 Andrew Sprott
-
- http://astronomy.crysania.co.uk
- astro@trefach.co.uk
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- }
-
- interface
-
- uses
- Windows,
- Messages,
- Variants,
- Classes,
- Graphics,
- Controls,
- Forms,
- Dialogs,
- StdCtrls,
- Buttons,
- CheckLst,
- ExtCtrls,
- SysUtils,
- inifiles,
-
- IdBaseComponent,
- IdComponent,
- IdTCPServer,
- IdResourceStrings,
- IdStack,
- IdGlobal,
- IdSocketHandle,
-
- ntc_server_focus,
- ntc_server_object,
- ntc_server_form;
-
- const
- seconds_in=60*60;
- seconds_in_az=360*seconds_in;
- seconds_in_alt=360*seconds_in;
- degrees_in_quarter=90;
- percent_100=65535;
- degrees_in_circle=360;
- minutes_in_degree=60;
- minutes_in_circle=degrees_in_circle*minutes_in_degree;
- minutes_in_semicircle=minutes_in_circle/2;
- hours_in_circle=24;
- minutes_in_hour=60;
- hours_minutes_in_circle=hours_in_circle*minutes_in_hour;
- hours_minutes_in_semicircle=hours_minutes_in_circle/2;
- local_machine=true;
- remote_machine=false;
- read_fail_count=2;
- { network }
- port_number=8383;
- crlf=chr(13)+chr(10);
- { correcting }
- default_intercept=99.834586;
- default_co_efficient_1=0.043221;
- default_co_efficient_2=-0.017952;
- default_co_efficient_3=5.71035432E-05;
-
- type
- button_type_set=(
- b_west,
- b_east,
- b_north,
- b_south);
- button_type=set of button_type_set;
-
- p_frame_record=^frame_record;
- frame_record=record
- level:integer;
- outer_frame,
- inner_frame:p_frame_record;
- frame_top,
- frame_left,
- frame_right,
- frame_base:integer;
- end;
-
- type
- Tscope_network = class(tform)
- network_panel: TPanel;
- ip_combo: TComboBox;
- refresh_button: TBitBtn;
- edit_port: TEdit;
- scope_server: TIdTCPServer;
- ip_address_label: TLabel;
- enable_network_check: TCheckBox;
- status_log: TMemo;
- clear_log_button: TBitBtn;
- messages_button: TBitBtn;
- io_messages_button: TBitBtn;
- disk_button: TBitBtn;
-
- { form handling }
- procedure formcreate(
- Sender:TObject);
-
- procedure kill;
-
- { scope commands }
- function connected
- :boolean;
-
- Procedure get_info(
- var Name:string;
- var can_query,
- can_sync,
- can_goto,
- can_slew,
- can_focus,
- can_track:boolean;
- var focuses:integer);
-
- { network stuff }
- procedure refresh_ip_list;
-
- procedure ip_comboChange(
- Sender: TObject);
-
- procedure start_server;
-
- procedure Stop_Server;
-
- procedure close_network_connection;
-
- procedure update_status_log(
- e:string;
- message_logging:boolean);
-
- procedure update_status_log_check(
- e:string);
-
- procedure change_button(
- button:button_type);
-
- { message handling }
- function process_input(
- AThread:TIdPeerThread;
- local:boolean;
- var object_record:tscope_object)
- :string;
-
- function send_message(
- message_string,
- error_string:string)
- :response_type;
-
- function send_message_check(
- message_string:string)
- :response_type;
-
- function get_option_value(
- option:string)
- :string;
-
- function get_float(
- option:string;
- var exit_result:double)
- :boolean;
-
- function get_integer(
- option:string;
- var exit_result:longint)
- :boolean;
-
- function get_string(
- option:string;
- var exit_result:string)
- :boolean;
-
- function get_boolean(
- option:string;
- var exit_result:boolean)
- :boolean;
-
- { utilities }
- function update_status_log_failed_check
- :boolean;
-
- procedure update_status_log_failed;
-
- procedure update_status_log_header(
- s:string);
-
- { configuration }
- procedure load_settings;
-
- procedure save_settings;
-
- { events }
- procedure FormShow(
- Sender: TObject);
-
- procedure adjust;
-
- procedure check_activate(
- Sender: TObject);
-
- procedure scope_serverExecute(
- AThread:TIdPeerThread);
-
- procedure edit_portChange(
- Sender: TObject);
-
- procedure refresh_buttonClick(
- Sender: TObject);
-
- procedure form_close_query(
- Sender: TObject;
- var CanClose: Boolean);
-
- procedure enable_network_checkClick(
- Sender: TObject);
-
- { logging }
- procedure clear_log_buttonClick(
- Sender: TObject);
-
- procedure messages_buttonClick(
- Sender: TObject);
-
- procedure io_messages_buttonClick(
- Sender: TObject);
-
- procedure disk_buttonClick(
- Sender: TObject);
-
- private
- { Private declarations }
- network_enabled,
- network_connected:boolean;
- received_text:widestring;
- ip_count:integer;
- response_text:string;
- option_names,
- option_values:tstrings;
- indented:integer;
- last_line_indented,
- last_line_unindented:boolean;
- fServerRunning:boolean;
- level:integer;
- very_first_frame:frame_record;
- current_frame:p_frame_record;
- current_x,
- current_y:double;
- public
- { Public declarations }
- server_playing:boolean;
- { configuration }
- dimensions:dimensions_record;
- returned_object:tscope_object;
- local_scope_control:boolean;
- message_logging,
- logging:boolean;
- { correcting }
- intercept:extended;
- co_efficient_1,
- co_efficient_2,
- co_efficient_3:extended;
- node_correct:boolean;
- { search }
- frame_level:integer;
-
- { events }
- procedure check_visible_and_show_hide(
- sender:tobject);
-
- procedure hide_form;
- procedure show_form;
-
- { slewing functions }
- procedure reset;
-
- function return_seconds(
- seconds:double)
- :double;
-
- function return_hours(
- degrees,
- y:double)
- :double;
-
- end;
-
- var
- scope_network:Tscope_network;
-
- implementation
-
- {$R *.dfm}
-
- Uses
- ntc_server_comms,
- ntc_server_config,
- ntc_server_info,
- ntc_server_observer,
- ntc_server_control,
- ntc_server_search;
-
- { -------------
- form handling
- ------------- }
-
- procedure tscope_network.formcreate(
- Sender:TObject);
- begin
- { network defaults }
- network_enabled:=false;
- network_connected:=false;
- server_playing:=false;
- ip_address_label.enabled:=false;
- ip_combo.enabled:=false;
- refresh_button.enabled:=false;
- edit_port.enabled:=false;
- local_scope_control:=true;
- returned_object:=nil;
- indented:=0;
- message_logging:=false;
- last_line_indented:=false;
- last_line_unindented:=false;
- intercept:=default_intercept;
- co_efficient_1:=default_co_efficient_1;
- co_efficient_2:=default_co_efficient_2;
- co_efficient_3:=default_co_efficient_3;
- refresh_ip_list;
- current_frame:=nil;
- load_settings;
- reset;
- // scope_control.flip_buttons(true);
- end;
-
- procedure Tscope_network.kill;
- begin
- close_network_connection;
- end;
-
- { --------------
- scope commands
- -------------- }
-
- function tscope_network.connected
- :boolean;
- begin
- if not scope_config.scope_enabled then
- result:=true
- else
- result:=scope_comms.scope_connected;
- end;
-
- Procedure tscope_network.get_info(
- var Name:string;
- var can_query,
- can_sync,
- can_goto,
- can_slew,
- can_focus,
- can_track:boolean;
- var focuses:integer);
- begin
- with scope_config do
- if (scope_type=lx200_type) or
- (scope_type=autostar_type) then
- begin
- name:=scope_config.model_list.text;
- can_query:=true;
- can_sync:=true;
- can_goto:=true;
- can_slew:=true;
- can_focus:=true;
- can_track:=true;
- focuses:=scope_focus.focus_speeds;
- end
- else
- begin
- name:=scope_config.model_list.text;
- can_query:=true;
- can_sync:=true;
- can_goto:=true;
- can_slew:=true;
- can_focus:=true;
- can_track:=true;
- focuses:=scope_focus.focus_speeds;
- end;
- end;
-
- { -------------
- network stuff
- ------------- }
-
- procedure tscope_network.refresh_ip_list;
- begin
- with ip_combo do
- begin
- Items := GStack.LocalAddresses;
- if items.IndexOf('127.0.0.1')<0 then
- Items.Insert(0, '127.0.0.1');
- ip_count:=items.count;
- end;
- end;
-
- procedure tscope_network.ip_comboChange(
- Sender: TObject);
- begin
- stop_server;
- start_server;
- end;
-
- procedure tscope_network.start_server;
- var
- Binding:TIdSocketHandle;
- i:integer;
- SL:TStringList;
- begin
- SL := TStringList.Create;
- try
- try
- { bindings cannot be cleared until TidTCPServer is inactive }
- scope_server.Bindings.Clear;
- for i := 0 to ip_count-1 do
- begin
- Binding:=scope_server.Bindings.Add;
- Binding.IP:=ip_combo.Items.Strings[i];
- Binding.Port:=StrToInt(edit_port.Text);
- SL.append('Server bound to IP '+Binding.IP+
- ' on port '+edit_port.Text);
- end;
- scope_server.Active:=true;
- fServerRunning:=scope_server.Active;
- status_log.lines.AddStrings(SL);
- update_status_log('Server started',true);
- if fserverrunning then
- update_status_log('Server running',true)
- else
- update_status_log('Server stopped',true);
- except
- on e:exception do
- begin
- update_status_log('Server not started : '+e.message,true);
- fServerRunning:=false;
- end;
- end;
- finally
- FreeAndNil(SL);
- end;
- end;
-
- procedure tscope_network.Stop_Server;
- begin
- if scope_server.active then
- begin
- scope_server.Active := false;
- scope_server.Bindings.Clear;
- fServerRunning:=not scope_server.Active;
- if fserverrunning then
- update_status_log('Server stopped',true)
- else
- update_status_log('Server running',true);
- end;
- end;
-
- procedure tscope_network.close_network_connection;
- begin
- network_enabled:=false;
- scope_server.active:=false;
- end;
-
- procedure tscope_network.update_status_log(
- e:string;
- message_logging:boolean);
- var
- s:string;
- a,m:boolean;
- begin
- if message_logging then
- begin
- a:=false;
- m:=false;
- if pos('>>',e)>0 then
- begin
- if not last_line_indented then
- inc(indented,2)
- else
- last_line_indented:=false;
- a:=true;
- end
- else if pos('<<',e)>0 then
- begin
- if not last_line_unindented then
- dec(indented,2)
- else
- last_line_unindented:=false;
- m:=true;
- end;
- s:=stringofchar(#32,indented)+e;
- if a then
- begin
- inc(indented,2);
- end
- else if m then
- begin
- dec(indented,2);
- end;
- status_log.Lines.Add(s+#13);
- scope_comms.write_log(s);
- end;
- end;
-
- procedure tscope_network.update_status_log_check(
- e:string);
- begin
- if message_logging then
- update_status_log(e,true);
- end;
-
- procedure tscope_network.update_status_log_header(
- s:string);
- begin
- update_status_log(s+' : ',message_logging);
- end;
-
- procedure tscope_network.update_status_log_failed;
- begin
- update_status_log_check(' !!! failed !!!');
- response_text:='fail';
- end;
-
- function tscope_network.update_status_log_failed_check
- :boolean;
- begin
- if pos(response_text,'fail')=1 then
- begin
- result:=true;
- update_status_log_failed;
- end
- else
- result:=false;
- end;
-
- { -----------------
- slewing functions
- ----------------- }
-
- procedure tscope_network.change_button(
- button:button_type);
- begin
- with scope_search do
- if button>=[b_north] then
- begin
- clock_button.glyph.LoadFromResourceName(hInstance,'NORTH_CLOCK');
- counter_button.glyph.LoadFromResourceName(hInstance,'NORTH_ANTI');
- end
- else if button>=[b_south] then
- begin
- clock_button.glyph.LoadFromResourceName(hInstance,'SOUTH_CLOCK');
- counter_button.glyph.LoadFromResourceName(hInstance,'SOUTH_ANTI');
- end
- else if button>=[b_east] then
- begin
- clock_button.glyph.LoadFromResourceName(hInstance,'EAST_CLOCK');
- counter_button.glyph.LoadFromResourceName(hInstance,'EAST_ANTI');
- end
- else if button>=[b_west] then
- begin
- clock_button.glyph.LoadFromResourceName(hInstance,'WEST_CLOCK');
- counter_button.glyph.LoadFromResourceName(hInstance,'WEST_ANTI');
- end;
- end;
-
- procedure tscope_network.reset;
- var
- t:integer;
- s:double;
- begin
- if scope_control.moving_scope then
- begin
- scope_control.moving_scope:=false;
- scope_search.disable_timer;
- end;
- with scope_search do
- begin
- t:=strtointdef(width_edit.text,-1);
- if t=-1 then
- begin
- width_edit.text:=inttostr(default_width);
- camera_width:=default_width;
- end
- else
- camera_width:=t;
- t:=strtointdef(height_edit.text,-1);
- if t=-1 then
- begin
- height_edit.text:=inttostr(default_height);
- camera_height:=default_height;
- end
- else
- camera_height:=t;
- s:=strtofloatdef(seconds_edit.text,-1);
- if s=-1 then
- begin
- seconds_edit.text:=floattostr(default_seconds);
- seconds_in_pixel:=default_seconds;
- end
- else
- seconds_in_pixel:=s;
- t:=strtointdef(timeout_edit.text,-1);
- if t=-1 then
- begin
- timeout_edit.text:=inttostr(default_timeout);
- timeout_interval:=default_timeout;
- end
- else
- timeout_interval:=t;
- search_timer.enabled:=false;
- if current_frame<>nil then
- begin
- while current_frame^.outer_frame<>nil do
- current_frame:=current_frame^.outer_frame;
- while current_frame^.inner_frame<>nil do
- begin
- current_frame:=current_frame^.inner_frame;
- dispose(current_frame^.outer_frame);
- end;
- end;
- level:=0;
- window_height:=camera_height;
- window_width:=camera_width;
- timeout_interval:=timeout_interval;
- height_part:=(100-camera_height_part)/100;
- width_part:=(100-camera_width_part)/100;
- with very_first_frame do
- begin
- inner_frame:=nil;
- outer_frame:=nil;
- level:=0;
- frame_top:=-(window_height div 2);
- frame_left:=-(window_width div 2);
- frame_right:=-frame_left;
- frame_base:=-frame_top;
- end;
- current_frame:=@very_first_frame;
- current_x:=0;
- current_y:=0;
- end;
- end;
-
- function tscope_network.return_seconds(
- seconds:double)
- :double;
- begin
- with scope_search do
- result:=seconds*seconds_in_pixel/seconds_in;
- end;
-
- function tscope_network.return_hours(
- degrees,
- y:double)
- :double;
- begin
- degrees:=degrees/(intercept+
- (co_efficient_1*y)+
- (co_efficient_2*(y*y))+
- (co_efficient_3*(y*y*y)))*100;
- result:=degrees/degrees_in_circle*hours_in_circle;
- end;
-
- function tscope_network.process_input(
- AThread:TIdPeerThread;
- local:boolean;
- var object_record:tscope_object)
- :string;
- var
- message_text,
- options_text,
- option_text,
- result_text,
- value_text:widestring;
- i,j:longint;
- s:string;
- options:boolean;
- done:boolean;
- arg_s_1,
- arg_s_2,
- arg_s_3:string;
- arg_i_1:integer;
- arg_b_1,
- arg_b_2,
- arg_b_3,
- arg_b_4,
- arg_b_5,
- arg_b_6:boolean;
- return_object:tscope_object;
- s_ra,s_dec,t_ra,t_dec:double;
-
- function find_option(
- option:string;
- var variable:string)
- :boolean;
- begin
- if option_names<>nil then
- begin
- i:=option_names.indexof(option);
- if i>=0 then
- begin
- variable:=option_values[i];
- result:=true;
- end
- else
- result:=false;
- end
- else
- result:=false;
- end;
-
- procedure add_option(
- name,
- value:string;
- bracket:boolean);
- begin
- result_text:=result_text+name+'='+value;
- if not bracket then
- result_text:=result_text+',';
- end;
-
- function booleantostr(
- bool:boolean)
- :string;
- begin
- if bool then
- result:='true'
- else
- result:='false';
- end;
-
- function query_high_precision
- :string;
- begin
- if scope_config.high_precision then
- result:='high'
- else
- result:='low';
- end;
-
- function ask(
- s:string)
- :boolean;
- begin
- response_text:=scope_comms.tell(s,return_object);
- result:=pos('ok',copy(response_text,1,2))>0;
- end;
-
- function get_ra_dec
- :boolean;
- begin
- with return_object do
- if ask('get_ra_dec') then
- begin
- s_ra:=ra/minutes_in_hour;
- s_dec:=dec/minutes_in_degree;
- result:=true;
- end
- else
- result:=false;
- end;
-
- procedure move_screen_left;
- begin
- with scope_search do
- begin
- if local_scope_control then
- disable_timer;
- if get_ra_dec then
- begin
- s_ra:=s_ra+return_hours(
- return_seconds(window_width*width_part),s_dec);
- with scope_network do
- begin
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- end
- else if local_scope_control then
- enable_timer;
- end;
- end;
- end;
- end;
-
- procedure move_screen_right;
- begin
- with scope_search do
- begin
- if local_scope_control then
- disable_timer;
- if get_ra_dec then
- begin
- s_ra:=s_ra-return_hours(
- return_seconds(window_width*width_part),s_dec);
- with scope_network do
- begin
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- end
- else if local_scope_control then
- enable_timer;
- end;
- end;
- end;
- end;
-
- procedure move_screen_up;
- begin
- with scope_search do
- begin
- if local_scope_control then
- disable_timer;
- if get_ra_dec then
- begin
- s_dec:=s_dec+return_seconds(window_height*height_part);
- with scope_network do
- begin
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- end
- else if local_scope_control then
- enable_timer;
- end;
- end;
- end
- end;
-
- procedure move_screen_down;
- begin
- with scope_search do
- begin
- if local_scope_control then
- disable_timer;
- if get_ra_dec then
- begin
- s_dec:=s_dec-return_seconds(window_height*height_part);
- with scope_network do
- begin
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- end
- else if local_scope_control then
- enable_timer;
- end;
- end;
- end
- end;
-
- function send
- :string;
- begin
- result:='nil';
- return_object:=nil;
- response_text:='ok';
- message_text:=received_text;
- trim(message_text);
- if message_text='ok' then
- exit;
- i:=pos('(',message_text);
- if i>0 then
- begin
- options:=true;
- options_text:=copy(message_text,i+1,length(message_text));
- if options_text[length(options_text)]<>')' then
- begin
- s:='No closing )'+crlf;
- if local then
- result:=s
- else
- athread.connection.writeln(s);
- exit;
- end;
- options_text:=copy(options_text,1,length(options_text)-1)+',';
- option_names:=tstringlist.create;
- option_values:=tstringlist.create;
- message_text:=copy(message_text,1,i-1);
- done:=false;
- while not done do
- begin
- i:=pos(',',options_text);
- if i>0 then
- begin
- option_text:=copy(options_text,1,i-1);
- options_text:=copy(options_text,i+1,length(options_text));
- j:=pos('=',option_text);
- if j>0 then
- begin
- value_text:=copy(option_text,j+1,length(option_text));
- option_text:=copy(option_text,1,j-1);
- end
- else
- value_text:='';
- option_names.add(option_text);
- option_values.add(value_text);
- end
- else
- done:=true;
- end;
- end
- else
- begin
- options:=false;
- option_names:=nil;
- option_values:=nil;
- end;
- arg_s_1:='debug';
- update_status_log_check('process_input >>');
- if message_text='play' then
- begin
- if not local_scope_control then
- begin
- update_status_log_header('play');
- result_text:='';
- add_option('model',inttostr(scope_config.model_list.itemindex),true);
- update_status_log_check('model=('+result_text+')');
- response_text:='ok=('+result_text+')';
- server_playing:=true;
- end
- else
- begin
- update_status_log('Enable network first',true);
- update_status_log_failed;
- end;
- end
- else if message_text='bored' then
- begin
- update_status_log_header('bored');
- if not server_playing then
- update_status_log_failed
- else
- server_playing:=false;
- end
- else if server_playing or local_scope_control then
- with scope_config do
- begin
- if message_text='connect' then
- begin
- update_status_log_header('connect');
- if not ask('connect') then
- update_status_log_failed;
- end
- else if message_text='disconnect' then
- begin
- update_status_log_header('disconnect');
- if not ask('disconnect') then
- update_status_log_failed
- else if local_scope_control then
- scope_control.change_panel(false)
- else
- scope_control.connect_group.enabled:=true;
- end
- else if message_text='connected' then
- begin
- update_status_log_header('connected');
- if connected then
- response_text:='true'
- else
- response_text:='false';
- response_text:='ok=(connected='+response_text+')';
- update_status_log_check(response_text);
- end
- else if message_text='align' then
- begin
- update_status_log_header('align');
- if find_option('ra',arg_s_1) and
- find_option('dec',arg_s_2) then
- begin
- s:='align(ra='+arg_s_1+',dec='+arg_s_2+')';
- update_status_log_check(s);
- if not ask(s) then
- response_text:='fail';
- end
- else
- response_text:='fail';
- update_status_log_failed_check;
- end
- else if message_text='get_ra_dec' then
- begin
- update_status_log_header('get_ra_dec');
- if ask('get_ra_dec') then
- with return_object do
- begin
- result_text:='';
- add_option('ra',floattostr(ra),false);
- add_option('dec',floattostr(dec),true);
- update_status_log_check('('+result_text+')');
- response_text:='ok=('+result_text+')';
- end
- else
- response_text:='fail';
- update_status_log_failed_check;
- end
- else if message_text='get_az_alt' then
- begin
- update_status_log_header('get_az_alt');
- if ask('get_az_alt') then
- with return_object do
- begin
- result_text:='';
- add_option('alt',floattostr(alt),false);
- add_option('az',floattostr(az),true);
- update_status_log_check('('+result_text+')');
- response_text:='ok=('+result_text+')';
- end
- else
- response_text:='fail';
- update_status_log_failed_check;
- end
- else if message_text='get_info' then
- begin
- update_status_log_header('get_info');
- get_info(arg_s_1,
- arg_b_1,arg_b_2,arg_b_3,arg_b_4,arg_b_5,arg_b_6,
- arg_i_1);
- result_text:='';
- add_option('name',arg_s_1,false);
- add_option('can_query',booleantostr(arg_b_1),false);
- add_option('can_sync',booleantostr(arg_b_2),false);
- add_option('can_goto',booleantostr(arg_b_3),false);
- add_option('can_slew',booleantostr(arg_b_4),false);
- add_option('can_focus',booleantostr(arg_b_5),false);
- add_option('can_track',booleantostr(arg_b_6),false);
- add_option('focus_speeds',inttostr(arg_i_1),true);
- update_status_log_check('('+result_text+')');
- response_text:='ok=('+result_text+')';
- end
- else if message_text='set_observer' then
- begin
- update_status_log_header('set_observer');
- if find_option('lat',arg_s_1) and
- find_option('long',arg_s_2) then
- begin
- update_status_log_check(
- '('+arg_s_1+' : '+arg_s_2+')');
- with scope_observer do
- set_observer(
- strtofloat(arg_s_1),
- strtofloat(arg_s_2),
- time_zone,
- now);
- end
- else
- update_status_log_failed;
- end
- else if message_text='query_high_precision' then
- begin
- update_status_log_header('query_high_precision');
- result_text:='';
- if scope_enabled then
- arg_s_1:=query_high_precision
- else
- arg_s_1:='low';
- add_option('query',arg_s_1,true);
- update_status_log_check('('+arg_s_1+')');
- response_text:='ok=('+result_text+')';
- end
- else if message_text='switch_precision' then
- begin
- result_text:='';
- update_status_log_header('switch_precision');
- if scope_enabled then
- begin
- if ask('switch_precision') then
- begin
- add_option('switch',query_high_precision,true);
- response_text:='ok=('+result_text+')';
- update_status_log_check(response_text);
- end
- else
- update_status_log_failed;
- end;
- end
- else if message_text='stop_focus' then
- begin
- update_status_log_header('stop_focus');
- if not ask('stop_focus') then
- response_text:='fail';
- update_status_log_failed_check;
- end
- else if message_text='focus' then
- begin
- update_status_log_header('focus');
- result_text:='';
- if find_option('dir',arg_s_1) and
- find_option('speed',arg_s_2) and
- find_option('timeout',arg_s_3) then
- begin
- s:='focus(dir='+arg_s_1[1]+
- ',speed='+arg_s_2+
- ',timeout='+arg_s_3+')';
- if not ask(s)
- then response_text:='fail';
- end
- else
- response_text:='fail';
- if not update_status_log_failed_check then
- update_status_log_check(s);
- end
- else if message_text='goto' then
- begin
- update_status_log_header('goto');
- arg_s_1:='debug-1';
- arg_s_2:='debug-2';
- if find_option('ra',arg_s_1) and
- find_option('dec',arg_s_2) then
- begin
- s:='goto_ra_dec(ra='+arg_s_1+',dec='+arg_s_2+')';
- if not ask(s) then
- response_text:='fail';
- end
- else if find_option('az',arg_s_1) and
- find_option('alt',arg_s_2) then
- begin
- s:='goto_az_alt(az='+arg_s_1+',alt='+arg_s_2+')';
- if not ask(s) then
- response_text:='fail';
- end
- else
- response_text:='fail';
- if not update_status_log_failed_check then
- update_status_log_check(s);
- end
- else if message_text='stop' then
- begin
- update_status_log_header('stop');
- if scope_enabled then
- begin
- arg_s_1:='stop_all';
- if (response_text<>'fail') and
- not ask(arg_s_1) then
- response_text:='fail';
- end;
- if not update_status_log_failed_check then
- update_status_log_check('('+arg_s_1+')');
- end
- else if (message_text='increase_tracking') or
- (message_text='decrease_tracking') or
- (message_text='lunar_tracking') or
- (message_text='default_tracking') then
- begin
- update_status_log_header(message_text);
- if not ask(message_text) then
- response_text:='fail';
- update_status_log_failed_check;
- end
- else if message_text='stopped' then
- begin
- result_text:='';
- if ask(message_text) then
- begin
- add_option('moving',booleantostr(return_object.moving),true);
- response_text:='ok=('+result_text+')';
- end
- else response_text:='fail';
- end
- else if message_text='move_screen_down' then
- move_screen_down
- else if message_text='move_screen_up' then
- move_screen_up
- else if message_text='move_screen_left' then
- move_screen_left
- else if message_text='move_screen_right' then
- move_screen_right
- else if message_text='move_left' then
- with scope_control,scope_search do
- begin
- if local_scope_control then
- scope_search.disable_timer;
- if get_ra_dec then
- begin
- find_option('distance',arg_s_1);
- t_ra:=strtofloatdef(arg_s_1,0);
- if t_ra>0 then
- begin
- if north_label.caption='W' then
- begin
- s_ra:=s_ra-return_hours(t_ra,s_dec);
- if s_ra<0 then
- s_ra:=s_ra+max_degrees;
- end
- else
- begin
- s_ra:=s_ra+return_hours(t_ra,s_dec);
- if s_ra>=max_degrees then
- s_ra:=s_ra-max_degrees;
- end;
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- write_status_log('west - failed to move scope');
- end
- else if local_scope_control then
- scope_search.enable_timer;
- end
- else write_status_log('west - wrong parameter');
- end
- else
- write_status_log('west - failed to read position');
- end
- else if message_text='move_right' then
- with scope_control,scope_search do
- begin
- if local_scope_control then
- scope_search.disable_timer;
- if get_ra_dec then
- begin
- find_option('distance',arg_s_1);
- t_ra:=strtofloatdef(arg_s_1,0);
- if t_ra>0 then
- begin
- if north_label.caption='E' then
- begin
- s_ra:=s_ra+return_hours(t_ra,s_dec);
- if s_ra<0 then
- s_ra:=s_ra+max_degrees;
- end
- else
- begin
- s_ra:=s_ra-return_hours(t_ra,s_dec);
- if s_ra>=max_degrees then
- s_ra:=s_ra-max_degrees;
- end;
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- write_status_log('east - failed to move scope');
- end
- else if local_scope_control then
- scope_search.enable_timer;
- end
- else
- write_status_log('east - wrong parameter');
- end
- else
- write_status_log('east - failed to read position');
- end
- else if message_text='move_up' then
- with scope_control do
- begin
- if local_scope_control then
- scope_search.disable_timer;
- if get_ra_dec then
- begin
- find_option('distance',arg_s_1);
- t_dec:=strtofloatdef(arg_s_1,0);
- if t_dec>0 then
- begin
- if north_label.caption='S' then
- begin
- s_dec:=s_dec-t_dec;
- if s_dec<=-max_degrees_dec then
- s_dec:=-max_degrees_dec-1;
- end
- else
- begin
- s_dec:=s_dec+t_dec;
- if s_dec>=max_degrees_dec then
- s_dec:=max_degrees_dec-1;
- end;
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- write_status_log('north - failed to move scope');
- end
- else if local_scope_control then
- scope_search.enable_timer;
- end
- else
- write_status_log('north - wrong parameter');
- end
- else
- write_status_log('north - failed to read position');
- end
- else if message_text='move_down' then
- with scope_control do
- begin
- if local_scope_control then
- scope_search.disable_timer;
- if get_ra_dec then
- begin
- find_option('distance',arg_s_1);
- t_dec:=strtofloatdef(arg_s_1,0);
- if t_dec>0 then
- begin
- if north_label.caption='N' then
- begin
- s_dec:=s_dec-t_dec;
- if s_dec<=-max_degrees_dec then
- s_dec:=-max_degrees_dec-1;
- end
- else
- begin
- s_dec:=s_dec+t_dec;
- if s_dec>=max_degrees_dec then
- s_dec:=max_degrees_dec-1;
- end;
- s:='goto_ra_dec(ra='+floattostr(s_ra)+
- ',dec='+floattostr(s_dec)+')';
- if not ask(s) then
- begin
- response_text:='fail';
- scope_control.moving_scope:=false;
- write_status_log('south - failed to move scope');
- end
- else if local_scope_control then
- scope_search.enable_timer;
- end
- else
- write_status_log('south - wrong parameter');
- end
- else
- write_status_log('south - failed to read position');
- end
- else if message_text='break_out' then
- begin
- with scope_search do
- begin
- with current_frame^ do
- begin
- if outer_frame=nil then
- begin
- new(outer_frame);
- outer_frame.frame_top:=
- frame_top-trunc(window_height*height_part);
- outer_frame.frame_left:=
- frame_left-trunc(window_width*width_part);
- outer_frame.frame_right:=
- frame_right+trunc(window_width*width_part);
- outer_frame.frame_base:=
- frame_base+trunc(window_height*height_part);
- outer_frame.inner_frame:=current_frame;
- outer_frame.outer_frame:=nil;
- outer_frame.level:=level+1;
- end;
- current_frame:=outer_frame
- end;
- with current_frame^ do
- begin
- result_text:='';
- if current_x<current_y then
- begin
- current_x:=frame_left+window_width/2;
- move_screen_left;
- change_button([b_west]);
- result_text:='west';
- end
- else if current_x>current_y then
- begin
- current_x:=frame_right-window_width/2;
- move_screen_right;
- change_button([b_east]);
- result_text:='east';
- end
- else if current_y<current_x then
- begin
- current_y:=frame_top+window_height/2;
- move_screen_up;
- change_button([b_north]);
- result_text:='north';
- end
- else
- begin
- current_y:=frame_base-window_height/2;
- move_screen_down;
- change_button([b_south]);
- result_text:='south';
- end;
- end;
- if result_text<>'' then
- response_text:='ok=(button='+result_text+')';
- end;
- end
- else if message_text='fall_in' then
- begin
- with scope_search do
- begin
- if current_frame^.level>0 then
- begin
- current_frame:=current_frame^.inner_frame;
- with current_frame^ do
- begin
- if current_x>frame_right then
- begin
- current_x:=frame_right-window_width/2;
- move_screen_left;
- end
- else if current_x<frame_left then
- begin
- current_x:=frame_left+window_width/2;
- move_screen_right;
- end;
- if current_y>frame_base then
- begin
- current_y:=frame_base-window_height/2;
- move_screen_up;
- end
- else if current_y<frame_top then
- begin
- current_y:=frame_top+window_height/2;
- move_screen_down;
- end;
- end;
- end;
- end;
- end
- else if message_text='clockwise' then
- begin
- if current_frame^.level>0 then
- with current_frame^.inner_frame^,scope_search do
- begin
- result_text:='';
- if current_y>frame_base then
- begin
- if current_x<frame_left then
- begin
- current_y:=current_y-window_height*height_part;
- change_button([b_west]);
- result_text:='west';
- move_screen_up;
- end
- else
- begin
- if current_x>frame_right then
- begin
- change_button([b_south]);
- result_text:='south';
- end;
- current_x:=current_x-window_width*width_part;
- move_screen_left;
- end;
- end
- else if current_y<frame_top then
- begin
- if current_x>frame_right then
- begin
- current_y:=current_y+window_height*height_part;
- change_button([b_east]);
- result_text:='east';
- move_screen_down;
- end
- else
- begin
- if current_x<frame_left then
- begin
- change_button([b_north]);
- result_text:='north';
- end;
- current_x:=current_x+window_width*width_part;
- move_screen_right;
- end;
- end
- else if current_x>frame_right then
- begin
- if current_y>frame_base then
- begin
- current_x:=current_x-window_width*width_part;
- change_button([b_south]);
- result_text:='south';
- move_screen_left;
- end
- else
- begin
- current_y:=current_y+window_height*height_part;
- move_screen_down;
- end;
- end
- else if current_y<frame_top then
- begin
- current_x:=current_x+window_width*width_part;
- change_button([b_north]);
- result_text:='north';
- move_screen_right;
- end
- else
- begin
- current_y:=current_y-window_height*height_part;
- move_screen_up;
- end;
- if result_text<>'' then
- response_text:='ok=(button='+result_text+')';
- end;
- end
- else if message_text='counter' then
- begin
- if current_frame^.level>0 then
- with current_frame^.inner_frame^,scope_search do
- begin
- result_text:='';
- if current_y<frame_top then
- begin
- if current_x<frame_left then
- begin
- current_y:=current_y+window_height*height_part;
- change_button([b_west]);
- result_text:='west';
- move_screen_down;
- end
- else
- begin
- if current_x>frame_right then
- begin
- change_button([b_north]);
- result_text:='north';
- end;
- current_x:=current_x-window_width*width_part;
- move_screen_left;
- end;
- end
- else if current_y>frame_base then
- begin
- if current_x>frame_right then
- begin
- current_y:=current_y-window_height*height_part;
- change_button([b_east]);
- result_text:='east';
- move_screen_up;
- end
- else
- begin
- if current_x<frame_left then
- begin
- change_button([b_south]);
- result_text:='south';
- end;
- current_x:=current_x+window_width*width_part;
- move_screen_right;
- end;
- end
- else if current_x<frame_left then
- begin
- if current_y>frame_base then
- begin
- current_x:=current_x+window_width*width_part;
- change_button([b_south]);
- result_text:='south';
- move_screen_right;
- end
- else
- begin
- current_y:=current_y+window_height*height_part;
- move_screen_down;
- end;
- end
- else if current_y<frame_top then
- begin
- current_x:=current_x-window_width*width_part;
- change_button([b_north]);
- result_text:='north';
- move_screen_left;
- end
- else
- begin
- current_y:=current_y-window_height*height_part;
- move_screen_up;
- end;
- if result_text<>'' then
- response_text:='ok=(button='+result_text+')';
- end;
- end
- else if message_text='reset_search' then
- begin
- reset;
- response_text:='ok';
- end
- else
- response_text:='fail';
- end
- else
- response_text:='fail';
- result:=response_text;
- if not local then
- athread.connection.writeln(response_text+crlf);
- received_text:='';
- update_status_log_check('<< process_input');
- object_record:=return_object;
- if return_object<>nil then
- return_object.free;
- end;
-
- begin
- result:=send;
- if options then
- begin
- option_names.free;
- option_values.free;
- end;
- end;
-
- { -------------------
- internal networking
- ------------------- }
-
- function tscope_network.send_message(
- message_string,
- error_string:string)
- :response_type;
- var
- e,s,t:string;
- done,
- f:boolean;
- i:integer;
- begin
- indented:=0;
- update_status_log_check('send_message >>');
- result:=[exit_ok];
- e:='i said : '+message_string;
- received_text:=message_string;
- f:=false;
- s:=process_input(nil,local_machine,returned_object);
- if s='nil' then
- begin
- f:=true;
- e:=e+' : and received a null repsonse : '+error_string;
- end
- else if s<>'' then
- begin
- if pos('ok',s)=1 then
- begin
- if pos('=',s)=3 then
- begin
- if pos('(',s)=4 then
- begin
- if option_names=nil then
- begin
- option_names:=tstringlist.create;
- option_values:=tstringlist.create;
- end;
- s:=copy(s,5,length(s));
- s[length(s)]:=',';
- option_names.clear;
- option_values.Clear;
- done:=false;
- while not done do
- begin
- i:=pos(',',s);
- if i>0 then
- begin
- t:=copy(s,1,i-1);
- s:=copy(s,i+1,length(s));
- i:=pos('=',t);
- option_names.Add(copy(t,1,i-1));
- option_values.add(copy(t,i+1,length(t)));
- end
- else
- done:=true;
- end;
- end
- end;
- end
- else if pos('fail',s)=1 then
- begin
- f:=true;
- e:=e+' : but reported : '+error_string;
- end
- end;
- if f then
- begin
- with scope_control do
- begin
- ignore_event_north:=event_0;
- ignore_event_south:=event_0;
- ignore_event_west:=event_0;
- ignore_event_east:=event_0;
- end;
- result:=[exit_fail];
- end;
- update_status_log_check(e);
- update_status_log_check('<< send_message');
- end;
-
- function tscope_network.send_message_check(
- message_string:string)
- :response_type;
- begin
- result:=send_message(message_string,no_response);
- end;
-
- function tscope_network.get_option_value(
- option:string)
- :string;
- var
- i:integer;
- begin
- i:=option_names.indexof(option);
- if i>=0 then
- result:=option_values[i]
- else
- result:='';
- end;
-
- function tscope_network.get_float(
- option:string;
- var exit_result:double)
- :boolean;
- var
- s:string;
- begin
- s:=get_option_value(option);
- if s<>'' then
- begin
- exit_result:=strtofloat(s);
- result:=true;
- end
- else
- result:=false;
- end;
-
- function tscope_network.get_integer(
- option:string;
- var exit_result:longint)
- :boolean;
- var
- s:string;
- begin
- s:=get_option_value(option);
- if s<>'' then
- begin
- exit_result:=strtoint(s);
- result:=true;
- end
- else
- result:=false;
- end;
-
- function tscope_network.get_string(
- option:string;
- var exit_result:string)
- :boolean;
- var
- s:string;
- begin
- s:=get_option_value(option);
- if s<>'' then
- begin
- exit_result:=s;
- result:=true;
- end
- else
- result:=false;
- end;
-
- function tscope_network.get_boolean(
- option:string;
- var exit_result:boolean)
- :boolean;
- var
- s:string;
- begin
- s:=get_option_value(option);
- if s<>'' then
- begin
- exit_result:=s='true';
- result:=true;
- end
- else
- result:=false;
- end;
-
- { -----------------------
- configuration functions
- ----------------------- }
-
- procedure tscope_network.load_settings;
- begin
- ini_file:=tinifile.create(application_path+'server.ini');
- with ini_file do
- begin
- message_logging:=ReadBool('network','message_logging',false);
- { form }
- scope.get_dimensions(scope_network,@dimensions,'network',ini_file);
- left:=dimensions.form_left;
- top:=dimensions.form_top;
- visible:=readbool('network','visible',false);
- end;
- ini_file.Free;
- end;
-
- procedure tscope_network.save_settings;
- begin
- with ini_file do
- begin
- writeBool('network','message_logging',message_logging);
- { form }
- scope.find_vdu(scope_network,@dimensions);
- scope.write_dimensions(@dimensions,left,top,'network',ini_file);
- writebool('network','visible',visible);
- end;
- end;
-
- { ------
- events
- ------ }
-
- procedure Tscope_network.FormShow(
- Sender: TObject);
- begin
- with dimensions do
- begin
- top:=form_top;
- left:=form_left;
- end;
- if scope_comms.file_logging then
- disk_button.Font.style:=[fsbold]
- else
- disk_button.font.style:=[];
- if scope_comms.io_logging then
- io_messages_button.Font.style:=[fsbold]
- else
- io_messages_button.font.style:=[];
- if message_logging then
- messages_button.Font.style:=[fsbold]
- else
- messages_button.font.style:=[];
- end;
-
- procedure tscope_network.adjust;
- begin
- with dimensions do
- begin
- form_top:=trunc(form_top/last_screen_height*current_height);
- form_left:=trunc(form_left/last_screen_width*current_width);
- end;
- if visible then
- show;
- end;
-
- procedure tscope_network.check_visible_and_show_hide(
- sender:tobject);
- begin
- if visible then
- hide_form
- else
- show_form;
- scope.show_hide(sender,visible);
- end;
-
- procedure tscope_network.hide_form;
- begin
- with dimensions do
- begin
- form_top:=top;
- form_left:=left;
- end;
- Visible:=false;
- end;
-
- procedure tscope_network.show_form;
- begin
- Visible:=true;
- end;
-
- procedure Tscope_network.check_activate(
- Sender: TObject);
- begin
- scope.form_activate(scope_network,@dimensions);
- end;
-
- procedure Tscope_network.form_close_query(
- Sender: TObject;
- var CanClose: Boolean);
- begin
- canclose:=false;
- visible:=false;
- with dimensions do
- begin
- form_top:=top;
- form_left:=left;
- end;
- end;
-
- procedure tscope_network.scope_serverExecute(
- AThread:TIdPeerThread);
- var
- returned_object:tscope_object;
- begin
- received_text:=athread.connection.readln(
- crlf,read_fail_count,max_buffer_size);
- if received_text<>'' then
- begin
- indented:=0;
- update_status_log_check(
- 'start remote command : '+FormatDateTime('hh:mm:ss.zzz',now));
- process_input(athread,remote_machine,returned_object);
- update_status_log_check(
- 'end remote command : '+FormatDateTime('hh:mm:ss.zzz',now));
- end;
- end;
-
- procedure tscope_network.edit_portChange(
- Sender: TObject);
- begin
- stop_server;
- start_server;
- end;
-
- procedure tscope_network.refresh_buttonClick(
- Sender: TObject);
- begin
- refresh_ip_list;
- end;
-
- procedure Tscope_network.enable_network_checkClick(
- Sender: TObject);
- begin
- with enable_network_check do
- begin
- scope_control.connect_group.enabled:=not checked;
- if not checked then
- begin
- server_playing:=false;
- stop_server;
- end
- else scope_control.disconnect_from_scope;
- local_scope_control:=not checked;
- ip_address_label.enabled:=checked;
- ip_combo.enabled:=checked;
- refresh_button.enabled:=checked;
- edit_port.enabled:=checked;
- end;
- end;
-
- { --------------
- logging events
- -------------- }
-
- procedure tscope_network.clear_log_buttonClick(
- Sender: TObject);
- begin
- status_log.Lines.Clear;
- update_status_log_check('log cleared');
- end;
-
- procedure tscope_network.messages_buttonClick(
- Sender: TObject);
- begin
- if not message_logging then
- begin
- message_logging:=true;
- messages_button.font.style:=[fsbold];
- update_status_log_check('messages enabled');
- end
- else
- begin
- messages_button.font.style:=[];
- update_status_log_check('messages disabled');
- message_logging:=false;
- end;
- end;
-
- procedure tscope_network.io_messages_buttonClick(
- Sender: TObject);
- begin
- with scope_comms do
- if not io_logging then
- begin
- io_logging:=true;
- io_messages_button.font.style:=[fsbold];
- update_status_log_check('IO messages enabled');
- end
- else
- begin
- io_messages_button.font.style:=[];
- update_status_log_check('IO messages disabled');
- io_logging:=false;
- end;
- end;
-
- procedure tscope_network.disk_buttonClick(
- Sender: TObject);
- begin
- with scope_network do
- begin
- if logging then
- begin
- logging:=false;
- disk_button.font.style:=[];
- update_status_log_check('logging disabled');
- end
- else
- begin
- logging:=true;
- scope_comms.init_log_file('ntc-client.log');
- disk_button.font.style:=[fsbold];
- update_status_log_check('logging enabled');
- end;
- end;
- end;
-
- end.
-